const cipher = require('util-cipher');
module.exports = app => {
  class accountService extends app.Service {
    // 登录
    async login(username, passwd) {
      const userObj = await this.ctx.model.User.find({ user: username, passwd });
      return userObj;
    }
    // 初始化系统
    async init() {
      this.ctx.model.User({
        user: 'admin',
        passwd: cipher.md5['+']('admin123'),
      });
      return 'test';
    }
    // 更新密码
    async changePwd(oldPwd, newPwd, username) {
      const userObj = await this.ctx.model.User.find({ user: username, passwd: oldPwd });
      if (!userObj) {
        return false;
      }
      this.ctx.model.User.findOneAndUpdate({ user: username, passwd: oldPwd }, { $set: { passwd: newPwd } });
      return true;
    }
  }
  return accountService;
};
